/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormStatistics.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "forms/FormStatistics.h"
#include "SCFormManager.h"
#include "SCNavigator.h"
#include "models/SCSession.h"
#include "models/SCHelpers.h"
#include "SCSettings.h"
#include "SCTranslator.h"

FormStatistics::FormStatistics()
{
}

FormStatistics::~FormStatistics()
{
}

bool
FormStatistics::Initialize(void)
{
	Construct(L"IDF_STATISTICS");
	return true;
}

result
FormStatistics::OnInitializing(void)
{
	//set MainMenu
	SCNavigator::SetMainMenu(this, GetTab(), SCFormManager::IDF_STATISTICS);

	this->pScrollPanel = static_cast<ScrollPanel*>(GetControl("IDC_SCROLLPANEL_STATISTICS"));
	this->pSleepPeriod = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_SLEEP_PERIOD"));
	this->pSleepLength = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_SLEEP_LENGTH"));
	this->pWishSleepLength = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_WISH_LENGTH"));
	this->pDifference = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_DIFFERENCE"));
	this->pSleepQuality = static_cast<EditField*>(this->pScrollPanel->GetControl("IDC_EDITFIELD_QUALITY"));

	this->pSleepPeriod->SetFocusable(false);
	this->pSleepLength->SetFocusable(false);
	this->pWishSleepLength->SetFocusable(false);
	this->pDifference->SetFocusable(false);
	this->pSleepQuality->SetFocusable(false);

	//language
	this->pSleepPeriod->SetTitleText(SCTranslator::GetText("IDS_STATISTICS_SLEEPPERIOD"));
	this->pSleepLength->SetTitleText(SCTranslator::GetText("IDS_STATISTICS_SLEEPLENGTH"));
	this->pWishSleepLength->SetTitleText(SCTranslator::GetText("IDS_STATISTICS_WISHSLEEPLENGTH"));
	this->pDifference->SetTitleText(SCTranslator::GetText("IDS_STATISTICS_DIFFERENCE"));
	this->pSleepQuality->SetTitleText(SCTranslator::GetText("IDS_STATISTICS_SLEEPQUALITY"));

	//this->Update();

	return E_SUCCESS;
}

result
FormStatistics::OnTerminating(void)
{

	return E_SUCCESS;
}

void
FormStatistics::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch (actionId)
	{
		case SCFormManager::IDF_ALARM:
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM, false, null, SCFormManager::NO_SLIDE);
			break;
		case SCFormManager::IDF_SETTINGS:
			SCFormManager::SwitchToForm(SCFormManager::IDF_SETTINGS, false, null, SCFormManager::NO_SLIDE);
			break;
		case SCFormManager::IDF_CATCH_SLEEP:
			SCFormManager::SwitchToForm(SCFormManager::IDF_CATCH_SLEEP, false, null, SCFormManager::NO_SLIDE);
			break;
		case SCFormManager::IDF_SESSIONS:
			SCFormManager::SwitchToForm(SCFormManager::IDF_SESSIONS, false, null, SCFormManager::NO_SLIDE);
			break;
		default:
			break;
	}
}

int
FormStatistics::GetFormId()
{
	return SCFormManager::IDF_STATISTICS;
}

void
FormStatistics::Update()
{
	SCSession* pSession;
	String sleepPeriodStr, wishSleepLengthStr, sleepLengthStr, sleepQualityStr, differenceStr;
	int sleepQuality = 0, sleepLength = 0, sessionCount = 0;
	float sleepLengthFinally, wishSleepLengthFinally;
	int floatPointCount = 3, sleepQualityItem = 0;

	// load session
	ArrayList* pSessionList = SCSession::GetAllSessions();

	if(pSessionList != NULL)
	{
		sessionCount = pSessionList->GetCount();
		for(int i = 0; i < sessionCount; i++)
		{
			pSession = static_cast<SCSession*>(pSessionList->GetAt(i));
			sleepLength += SCHelpers::GetTimeDifference(pSession->GetStartDate(), pSession->GetStopDate());

			if(pSession->GetQualityOfSleep() > 0)
			{
				sleepQuality += pSession->GetQualityOfSleep();
				sleepQualityItem++;
			}
		}

		sleepLengthFinally = SCHelpers::Round(SCHelpers::ConvertMinuteToHours(sleepLength / (float) sessionCount / (float) TimeSpan::NUM_OF_TICKS_IN_MINUTE), floatPointCount);
		wishSleepLengthFinally = SCHelpers::Round(SCHelpers::ConvertMinuteToHours(SCSettings::GetSleepLength()), floatPointCount);

		sleepLengthStr.SetLength(floatPointCount + 1);
		wishSleepLengthStr.SetLength(floatPointCount + 1);
		if(sleepLengthFinally > wishSleepLengthFinally)
		{
			differenceStr.SetLength(floatPointCount + 1);
		} else {
			differenceStr.SetLength(floatPointCount + 2);
		}

		if(sleepQualityItem > 0)
		{
			sleepQualityStr.Append(sleepQuality / (float) sleepQualityItem);
		} else {
			sleepQualityStr.Append("-");
		}

		sleepPeriodStr.Append(SCSettings::GetSleepPeriod());
		wishSleepLengthStr.Append(wishSleepLengthFinally);
		sleepLengthStr.Append(sleepLengthFinally);
		differenceStr.Append(sleepLengthFinally - wishSleepLengthFinally);
	} else {
		sleepPeriodStr.Append(SCSettings::GetSleepPeriod());
		wishSleepLengthStr.Append(SCHelpers::Round(SCHelpers::ConvertMinuteToHours(SCSettings::GetSleepLength()), floatPointCount));
		sleepQualityStr.Append("-");
		sleepLengthStr.Append("-");
		differenceStr.Append("-");
	}

	this->pSleepPeriod->SetText(sleepPeriodStr);
	this->pSleepLength->SetText(sleepLengthStr);
	this->pWishSleepLength->SetText(wishSleepLengthStr);
	this->pDifference->SetText(differenceStr);
	this->pSleepQuality->SetText(sleepQualityStr);

	//Set active tab
	SCNavigator::SetActiveTab(this->GetTab(), SCFormManager::IDF_STATISTICS);
}

void
FormStatistics::Redraw()
{
	this->Update();
	RequestRedraw(true);
}
